package com.cr.front.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.front.system.mapper.SysDictMapper;
import com.cr.front.system.mapper.SysDictOptionMapper;
import com.cr.front.system.pojo.entity.SysDict;
import com.cr.front.system.pojo.entity.SysDictOption;
import com.cr.front.system.pojo.query.SysDictOptionQuery;
import com.cr.front.system.pojo.vo.SysDictOptionVO;
import com.cr.front.system.service.SysDictOptionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class sysDictOptionServiceImpl implements SysDictOptionService {

    @Autowired
    private SysDictOptionMapper sysDictOptionMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Override
    public List<SysDictOptionVO> selectList(SysDictOptionQuery query) {
        QueryWrapper<SysDictOption> queryWrapper= new QueryWrapper<>();
        if(query.getDictId()!=null){
            queryWrapper.eq("dict_id",query.getDictId());
            queryWrapper.last("order by sort");
        }else if(query.getCode() != null){
            QueryWrapper<SysDict> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.like("code",query.getCode());
            List<SysDict> list = sysDictMapper.selectList(queryWrapper1);
            if(!list.isEmpty()){
                queryWrapper.eq("dict_id",list.get(0).getId());
            }
        }


        List<SysDictOption> list = sysDictOptionMapper.selectList(queryWrapper);

        return buildSysOptionVOList(list);


    }

    private  List<SysDictOptionVO> buildSysOptionVOList(List<SysDictOption> list) {
        List<SysDictOptionVO> sysDictOptionVOS=new ArrayList<>();
        for (SysDictOption sysDictOption : list){
            SysDictOptionVO sysDictOptionVO=new SysDictOptionVO();
            BeanUtils.copyProperties(sysDictOption,sysDictOptionVO);
            sysDictOptionVOS.add(sysDictOptionVO);
        }
        return sysDictOptionVOS;
    }
}
